//
//  NumberpadViewController.m
//  SecretAlbums
//
//  Created by BabyFinancial on 15/5/21.
//  Copyright (c) 2015年 BabyFinancial. All rights reserved.
//

#import "NumberpadViewController.h"
#import "CircleButton.h"

#define CircleButtonDiameter Adapter_X(74.0f)
#define Spacing (CircleButtonDiameter + Adapter_X(18.0f))
#define OffsetOfY Adapter_Y(-14.0f)

@interface NumberpadViewController () <CircleButtonDelegate>

@property (nonatomic, strong) NSMutableArray *circleBtnArray;

@end

@implementation NumberpadViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.circleBtnArray = [NSMutableArray array];
    
    // 添加提示文本
    self.titleLable = [UILabel new];
    [self.titleLable setTranslatesAutoresizingMaskIntoConstraints:NO];
    self.titleLable.font = [UIFont systemFontOfSize:Adapter_X(22.0f)];
    [self.view addSubview:self.titleLable];
    
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view).centerOffset(CGPointMake(0, Iphone4OrOtherAdapter_Y(-190, -210)));
    }];
    
    // 添加数字按钮
    for (int i=0; i<10; i++) {
        CircleButton *btn = [[CircleButton alloc] initWithType:CircleButtonDefault];
        [btn setTranslatesAutoresizingMaskIntoConstraints:NO];
        btn.titleLabel.text = [NSString stringWithFormat:@"%d", i];
        btn.delegate = self;
        [self.view addSubview:btn];
        [self.circleBtnArray addObject:btn];
        
        if (0 == i) {
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(CircleButtonDiameter, CircleButtonDiameter));
                make.centerX.equalTo(self.view.mas_centerX);
                make.centerY.equalTo(self.view.mas_centerY).offset(2 * Spacing + OffsetOfY);
            }];
        } else {
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(CircleButtonDiameter, CircleButtonDiameter));
                make.centerX.equalTo(self.view.mas_centerX).offset(-Spacing + ((i-1)%3)*Spacing);
                make.centerY.equalTo(self.view.mas_centerY).offset(-Spacing + ((i-1)/3)*Spacing + OffsetOfY);
            }];
        }
    }
    
    // 添加取消按钮
    CircleButton *btn = [[CircleButton alloc] initWithType:CircleButtonCancel];
    [btn setTranslatesAutoresizingMaskIntoConstraints:NO];
    btn.delegate = self;
    [self.view addSubview:btn];
    [self.circleBtnArray addObject:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(CircleButtonDiameter, CircleButtonDiameter));
        make.centerX.equalTo(self.view.mas_centerX).offset(Spacing);
        make.centerY.equalTo(self.view.mas_centerY).offset(2 * Spacing + OffsetOfY);
    }];
    
    // 添加dot
    self.dotsView = [[DotsView alloc] initWithAmount:6];
    [self.dotsView setTranslatesAutoresizingMaskIntoConstraints:NO];
    self.dotsView.delegate = self;
    [self.view addSubview:self.dotsView];
    
    [self.dotsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view).centerOffset(CGPointMake(0, Iphone4OrOtherAdapter_Y(-160, -170)));
    }];
    
    // 添加action按钮
    self.actionBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.actionBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    self.actionBtn.titleLabel.font = [UIFont systemFontOfSize:Adapter_X(18.0f)];
    [self.view addSubview:self.actionBtn];
    
    [self.actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_left).offset(Adapter_X(60));
        make.centerY.equalTo(self.view.mas_bottom).offset(Adapter_Y(-40));
    }];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (void)setCircleBtnTitleColor:(UIColor *)circleBtnTitleColor
{
    _circleBtnTitleColor = circleBtnTitleColor;
    
    for (int i=0; i<self.circleBtnArray.count; i++) {
        CircleButton *btn = self.circleBtnArray[i];
        btn.titleLabel.textColor = self.circleBtnTitleColor;
        btn.cancelLayer.strokeColor = self.circleBtnTitleColor.CGColor;
    }
}

#pragma mark - CircleButtonDelegate

- (void)touchCircleButton:(CircleButton *)circleButton
{
    [self.dotsView operateFromCircleButton:circleButton];
}

@end
